import path from 'path';
import { createWorker } from 'tesseract.js';
import Log from './log.mjs';
import { CORE_PATH } from './config.js';

export default async (lang = 'chi_sim', target) => {
	const worker = createWorker({
		corePath: CORE_PATH,
		langPath: path.resolve(process.cwd(), 'assets'),
		workerBlobURL: false,
		logger: Log,
	});

	await worker.load();
	await worker.loadLanguage(lang);
	await worker.initialize(lang);

	const { data: { text } } = await worker.recognize(target);

	await worker.terminate();

	return text.toString();
};